using System.Collections.ObjectModel;
using System.ComponentModel;
using Newtonsoft.Json;

namespace DocumentManager.Models;

/// <summary>
/// 文档节点类型
/// </summary>
public enum DocumentNodeType
{
    Document,    // 根文档
    Title,       // 标题
    Content      // 内容
}

/// <summary>
/// 内容类型
/// </summary>
public enum ContentType
{
    Text,   // 文本内容
    Image   // 图片内容
}

/// <summary>
/// 文档节点基类
/// </summary>
public class DocumentNode : INotifyPropertyChanged
{
    private string _title = string.Empty;
    private DocumentNodeType _nodeType;
    private int _level;
    private bool _isExpanded = true;
    private bool _isSelected;

    /// <summary>
    /// 节点唯一标识
    /// </summary>
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 节点标题
    /// </summary>
    public string Title
    {
        get => _title;
        set
        {
            if (_title != value)
            {
                _title = value;
                OnPropertyChanged(nameof(Title));
            }
        }
    }

    /// <summary>
    /// 节点类型
    /// </summary>
    public DocumentNodeType NodeType
    {
        get => _nodeType;
        set
        {
            if (_nodeType != value)
            {
                _nodeType = value;
                OnPropertyChanged(nameof(NodeType));
            }
        }
    }

    /// <summary>
    /// 标题级别 (1-6)
    /// </summary>
    public int Level
    {
        get => _level;
        set
        {
            if (_level != value && value >= 1 && value <= 6)
            {
                _level = value;
                OnPropertyChanged(nameof(Level));
            }
        }
    }

    /// <summary>
    /// 是否展开
    /// </summary>
    [JsonIgnore]
    public bool IsExpanded
    {
        get => _isExpanded;
        set
        {
            if (_isExpanded != value)
            {
                _isExpanded = value;
                OnPropertyChanged(nameof(IsExpanded));
            }
        }
    }

    /// <summary>
    /// 是否选中
    /// </summary>
    [JsonIgnore]
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                OnPropertyChanged(nameof(IsSelected));
            }
        }
    }

    /// <summary>
    /// 父节点
    /// </summary>
    [JsonIgnore]
    public DocumentNode? Parent { get; set; }

    /// <summary>
    /// 子节点集合
    /// </summary>
    public ObservableCollection<DocumentNode> Children { get; set; } = new();

    /// <summary>
    /// 内容项集合
    /// </summary>
    public ObservableCollection<ContentItem> Contents { get; set; } = new();

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 修改时间
    /// </summary>
    public DateTime ModifiedTime { get; set; } = DateTime.Now;

    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        ModifiedTime = DateTime.Now;
    }

    /// <summary>
    /// 添加子节点
    /// </summary>
    public void AddChild(DocumentNode child)
    {
        child.Parent = this;
        Children.Add(child);
        OnPropertyChanged(nameof(Children));
    }

    /// <summary>
    /// 移除子节点
    /// </summary>
    public void RemoveChild(DocumentNode child)
    {
        child.Parent = null;
        Children.Remove(child);
        OnPropertyChanged(nameof(Children));
    }

    /// <summary>
    /// 添加内容项
    /// </summary>
    public void AddContent(ContentItem content)
    {
        Contents.Add(content);
        OnPropertyChanged(nameof(Contents));
    }

    /// <summary>
    /// 移除内容项
    /// </summary>
    public void RemoveContent(ContentItem content)
    {
        Contents.Remove(content);
        OnPropertyChanged(nameof(Contents));
    }

    /// <summary>
    /// 获取所有后代节点
    /// </summary>
    public IEnumerable<DocumentNode> GetAllDescendants()
    {
        foreach (var child in Children)
        {
            yield return child;
            foreach (var descendant in child.GetAllDescendants())
            {
                yield return descendant;
            }
        }
    }
} 